package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
@Api
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;


    //分页查询
    @GetMapping("/page")
    public R selectByPage(Integer page,Integer pageSize,String name){
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> lwq =new LambdaQueryWrapper<Dish>();
        lwq.like(name!=null&&name!="",Dish::getName,name);
        lwq.orderByDesc(Dish::getUpdateTime);
        lwq.eq(Dish::getIsDeleted,0);
        Page<Dish> page1 = dishService.page(dishPage, lwq);
        //把原有的Dish对象重新封装成一个dishDto对象
        List<Dish> dishes = page1.getRecords();
        //深克隆
        List<DishDto> dishDtos = JSON.parseArray(JSON.toJSONString(dishes), DishDto.class);
        for (DishDto dishDto : dishDtos) {
            Long categoryId = dishDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
        }
        Page<DishDto> dishDtoPage = new Page<>();
        dishDtoPage.setRecords(dishDtos);
        dishDtoPage.setTotal(page1.getTotal());
        return R.success(dishDtoPage);
    }

    //新增菜品
    @PostMapping
    public R dishAdd(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    //回显菜品和口味
    @GetMapping("/{id}")
    public R dishById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    //修改菜单
    @PutMapping
    public R dishUpdate(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("菜品修改成功");
    }

    //批量删除
    @DeleteMapping
    public R dishDeleteByIds(long[]ids){
        dishService.deleteWithFlavor(ids);
        return R.success("菜品删除成功");
    }
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable int status,long[]ids){
        dishService.updateStatus(status,ids);
        return R.success("修改状态成功");
    }

    //根据分类ID查询菜品列表
    @GetMapping("/list")
    public R list(Dish dish ){
        List<DishDto> list = dishService.categoryList(dish);
        return R.success(list);
    }


}
